﻿namespace Smart.Utils.Security2
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class DecryptTransformer
    {
        private Smart.Utils.Security2.EncryptionAlgorithm algorithmID;
        private bool bHasIV;
        private byte[] IV;
        private string SecurityKey;

        public DecryptTransformer(Smart.Utils.Security2.EncryptionAlgorithm algID)
        {
            this.SecurityKey = "";
            this.bHasIV = false;
            this.algorithmID = algID;
        }

        public DecryptTransformer(Smart.Utils.Security2.EncryptionAlgorithm algID, byte[] iv)
        {
            this.SecurityKey = "";
            this.bHasIV = false;
            this.algorithmID = algID;
            this.IV = iv;
            this.bHasIV = true;
        }

        public ICryptoTransform GetCryptoTransform()
        {
            bool flag = false;
            if (this.SecurityKey.Length != 0)
            {
                flag = true;
            }
            byte[] bytes = Encoding.ASCII.GetBytes(this.SecurityKey);
            switch (this.algorithmID)
            {
                case Smart.Utils.Security2.EncryptionAlgorithm.DES:
                {
                    DES des = new DESCryptoServiceProvider();
                    if (flag)
                    {
                        des.Key = bytes;
                    }
                    if (this.bHasIV)
                    {
                        des.IV = this.IV;
                    }
                    return des.CreateDecryptor();
                }
                case Smart.Utils.Security2.EncryptionAlgorithm.Rc2:
                {
                    RC2 rc = new RC2CryptoServiceProvider();
                    if (flag)
                    {
                        rc.Key = bytes;
                    }
                    if (this.bHasIV)
                    {
                        rc.IV = this.IV;
                    }
                    return rc.CreateDecryptor();
                }
                case Smart.Utils.Security2.EncryptionAlgorithm.Rijndael:
                {
                    Rijndael rijndael = new RijndaelManaged();
                    if (flag)
                    {
                        rijndael.Key = bytes;
                    }
                    if (this.bHasIV)
                    {
                        rijndael.IV = this.IV;
                    }
                    return rijndael.CreateDecryptor();
                }
                case Smart.Utils.Security2.EncryptionAlgorithm.TripleDes:
                {
                    TripleDES edes = new TripleDESCryptoServiceProvider();
                    if (flag)
                    {
                        edes.Key = bytes;
                    }
                    if (this.bHasIV)
                    {
                        edes.IV = this.IV;
                    }
                    return edes.CreateDecryptor();
                }
            }
            throw new CryptographicException("Algorithm ID '" + this.algorithmID + "' not supported.");
        }

        public void SetSecurityKey(string Key)
        {
            this.SecurityKey = Key;
        }

        public Smart.Utils.Security2.EncryptionAlgorithm EncryptionAlgorithm
        {
            get
            {
                return this.algorithmID;
            }
            set
            {
                this.algorithmID = value;
            }
        }
    }
}

